<?php

namespace app\home\controller;

use app\common\controller\Home;
use Error;

class Coupon extends Home
{
    protected $NoNeedLogin = ['index'];

    protected $CouponModel = null;

    protected $ReceiveModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->CouponModel = model('coupon.Coupon');

        $this->ReceiveModel = model('coupon.Receive');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');

            $where = [];

            $list = $this->CouponModel->where(['status' => '1'])->page($page, $limit)->select();
            // halt(collection($list)->toArray());

            $business = $this->IsLogin(false);         //跳过登录验证,获取当前用户的id

            $i = 1;
            if (!empty($business)) {
                foreach ($list as $item) {
                    $where = [
                        'cid' => $item['id'],
                        'busid' => $business['id']
                    ];

                    $receive = $this->ReceiveModel->where($where)->find();

                    if (!empty($receive)) {
                        $item['receive_status'] = true;
                    } else {
                        $item['receive_status'] = false;
                    }
                }
            }

            //查询数据总数
            $count = $this->CouponModel->where(['status' => '1'])->count();

            $data = [
                'list' => $list,
                'count' => $count
            ];

            if ($list) {
                $this->success('查询优惠卷成功', null, $data);
            } else {
                $this->error('暂无优惠卷', null, $data);
            }
        }
        return $this->fetch();
    }

    //优惠卷领取数量点击减少
    public function btn(){
        if ($this->request->isAjax()) {
            $btn = $this->request->param('receive', 'true', 'trim');
            $value = $this->request->param('values', 0, 'trim');

            $business = $this->IsLogin(false); 

            $data = [
                'cid' => $value,
                'busid' => $business['id'],
                'createtime' => time(),
                'status' => 1,
                'receive' => $btn
            ];

            $coupon = $this->CouponModel->validate('common/coupon/Total')->where(['id' => $value])->select();

            // halt(collection($coupon)->toArray());
            foreach (collection($coupon)->toArray() as $key) {
               
                $total = $key['total'] - 1;
            }
            // halt($total);
            $where = [
                // 'id' => $value,
                'total' => $total
            ];

            // halt($where);

            $list = $this->CouponModel->where(['id' => $value])->update($where);

            // halt($list);
            $receive = $this->ReceiveModel->validate('common/coupon/Index')->save($data);
            // halt($receive);
            if ($receive === false) {
                // 获取模型错误信息
                $this->error('领取失败');
            } else {
                $this->success('领取成功',null,$data);
            }
        }


        return $this->fetch();
    }


}
